package com.audaque.springboot.foshanupload.security.config;

import com.audaque.springboot.foshanupload.authcore.properties.LoginProperties;
import com.audaque.springboot.foshanupload.security.component.MyMd5PasswordEncoder;
import com.audaque.springboot.foshanupload.security.filter.MyCaptchaFilter;
import com.audaque.springboot.foshanupload.security.filter.MyJwtAuthenticationFilter;
import com.audaque.springboot.foshanupload.security.filter.MyUsernamePasswordAuthenticationFilter;
import com.audaque.springboot.foshanupload.security.handler.*;

import com.audaque.springboot.foshanupload.security.provider.MyUsernamePasswordAuthenticationProvider;
import com.audaque.springboot.foshanupload.security.service.impl.MyUserDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.Arrays;

/**
 * @author zgb
 * @desc ...
 * @date 2022-08-12 00:57:14
 */

@Configuration
public class SecurityBeanConfig {


    @Autowired
    private MyMd5PasswordEncoder myMd5PasswordEncoder;

    @Autowired
    private MyUserDetailService myUserDetailService;


    @Autowired
    private MyUrlAuthenticationFailureHandler myUrlAuthenticationFailureHandler;


    @Autowired
    private MyUrlAuthenticationSuccessHandler myAuthenticationSuccessHandler;


 /*   @Autowired
    private MyAccessDeniedHandler myAccessDeniedHandler;

*/
    @Autowired
    private MyLogoutSuccessHandler myLogoutSuccessHandler;





/*
    @Autowired
    private MyAuthenticationEntryPoint myAuthenticationEntryPoint;

*/



    @Autowired
    private LoginProperties loginProperties;


    @Autowired
    private MyUsernamePasswordAuthenticationProvider myUsernamePasswordAuthenticationProvider;





    /**
     * BCrypt是hash算法
     */
 /*   @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }

*/


    @Bean
    public MyUsernamePasswordAuthenticationFilter myUsernamePasswordAuthenticationFilter() {
        //自定义账号密码认证过滤器
        MyUsernamePasswordAuthenticationFilter myUsernamePasswordAuthenticationFilter = new MyUsernamePasswordAuthenticationFilter();

        //认证失败处理器
        myUsernamePasswordAuthenticationFilter.setAuthenticationFailureHandler(myUrlAuthenticationFailureHandler);

        //认证成功处理器
        myUsernamePasswordAuthenticationFilter.setAuthenticationSuccessHandler(myAuthenticationSuccessHandler);
        ////如果loginProcessingUrl还是“/login”的话走的是UsernamePasswordAuthenticationFilter
        myUsernamePasswordAuthenticationFilter.setFilterProcessesUrl(loginProperties.getLoginUrl());
        DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
        daoAuthenticationProvider.setPasswordEncoder(myMd5PasswordEncoder);
        daoAuthenticationProvider.setUserDetailsService(myUserDetailService);
        ProviderManager providerManager = new ProviderManager(Arrays.asList(myUsernamePasswordAuthenticationProvider, daoAuthenticationProvider));
        myUsernamePasswordAuthenticationFilter.setAuthenticationManager(providerManager);

        return myUsernamePasswordAuthenticationFilter;
    }



    @Bean
    public MyJwtAuthenticationFilter myJwtAuthenticationFilter( ) {
        //自定义jwt认证过滤器
        MyJwtAuthenticationFilter myJwtAuthenticationFilter = new MyJwtAuthenticationFilter();
        return myJwtAuthenticationFilter;
    }


    @Bean
    public MyCaptchaFilter myCaptchaFilter( ) {
        //自定义验证码过滤器
        MyCaptchaFilter myCaptchaFilter = new MyCaptchaFilter();
        return myCaptchaFilter;
    }






}
